# Python 2.6.4
# Project Euler, Problem 34
# Copyright 2010 Talha Zaman

def fac(n): return reduce(lambda x,y:x*y, range(1, n+1),1)
total = 0
for i in range(3, 100000):
    digits = sum([fac(int(x)) for x in str(i)])
    if digits == i: total += i
print total
